<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Promise的.then返回值详解</title>
</head>

<body>
  <script>
    // 创建promise对象
    const p = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve("成功了");
        // reject("失败了");
      }, 1000);
    });

    // 调用then方法
    const result = p.then(
      value => {
        console.log(value);
        // 返回结果状态
        // 1.非 Promise 对象类型
        // return "你好牛轧"; // PromiseState: "fulfilled", PromiseResult: "你好牛轧"

        // 2.Promise对象
        // return Promise.resolve("Promise成功"); // PromiseState: "fulfilled", PromiseResult: "Promise成功"
        // return Promise.reject("Promise失败"); // PromiseState: "rejected", PromiseResult: "Promise失败"

        // 3.抛出异常
        // throw new Error("出错了");
        // throw "出错了"; // PromiseState: "rejected", PromiseResult: "出错了"

        // 4.不返回
        // PromiseState: "fulfilled", PromiseResult: undefined
      },
      resaon => {
        console.log(resaon);
      }
    )
    // 返回的result也是一个Promise也就是说可以直接在.then()后面接着.then() 这样就可以链式调用了 避免回调地狱
    console.log(result);
  </script>
</body>

</html>